import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:u9mobile/models/template.dart';
import 'package:u9mobile/widgets/text_group.dart';

import '../blocs/template_bloc.dart';
import '../models/area.dart';
/// 子表可折叠
class BillDetail extends StatelessWidget {
  const BillDetail({super.key});
  /// 渲染表头
  buildCard(Area? area){
    return Card(
      margin: const EdgeInsets.fromLTRB(6, 15, 6, 15),
      elevation: 1, // 卡片的阴影
      color: Colors.transparent, // 设置背景颜色为白色
      shadowColor: Colors.transparent,
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Column(
          children: [
            Column(
              children: area!=null?area.items.map(
                  (item) => TextGroup(lebal: item.label,content: '1',)
                ).toList():List.empty()
            ),
            const Divider(
              height: 1,
            ),
            ConstrainedBox(
              constraints:const BoxConstraints(minWidth: double.infinity),
              child: TextButton(
                onPressed: () {},
                child: const Text('显示更多'),
              ),
            ),
          ],
        ),
      ),
    );
  }
  /// 渲染表体
  buildBodyList(List<Area>? body){
    if(body==null){
      return const SizedBox.shrink();
    }
    return ConstrainedBox(
      constraints: const BoxConstraints(maxHeight: 600),
      child: ListView.builder(
        itemCount: body.length,
        itemBuilder: (context, index) {
          return OpenContainer(
            transitionDuration: const Duration(seconds: 2),
            transitionType: ContainerTransitionType.fade,
            openBuilder: (context, action) {
              return Scaffold(
                appBar: AppBar(
                  title: Text(body[index].name),
                ),
                body: const Center(
                  child: Column(
                    children: [
                      SizedBox(
                        height: 200,
                        width: 200,
                        child: Icon(
                          Icons.cake,
                          size: 200,
                          color: Colors.orange,
                        ),
                      )
                    ],
                  ),
                ),
              );
            },
            closedBuilder: (context, openContainer) {
              return ExpansionTile(
                // backgroundColor: Colors.blue[100],
                title: GestureDetector(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        body[index].name,
                        style:const TextStyle(fontSize: 16),
                      ),
                      // Text('副标题',style: TextStyle(fontSize: 14,color: Colors.grey),)
                    ],
                  ),
                  onTap: () {
                    openContainer();
                  },
                ),
                children: [
                  ConstrainedBox(
                    constraints:const BoxConstraints(maxHeight: 200),
                    child: ListView.builder(
                      itemCount: body[index].items.length,
                      itemBuilder: (context, itemIndex) {
                        return ListTile(
                          title: TextGroup(lebal: body[index].items[itemIndex].label,content: '1',),
                        );
                      },
                    ),
                  )
                ],
              );
            },
          );
        },
      ),
    );
  }
  buildBodyDetail(){

  }
  final SnackBar httpToast = const SnackBar(
    content: Text('审批成功',style: TextStyle(color: Colors.white),),
    backgroundColor: Colors.blue,
  );
  @override
  Widget build(BuildContext context) {
    final TemplateBloc templateBloc=BlocProvider.of(context);
    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: BlocBuilder<TemplateBloc,Template>(
              builder: (context, state){
                return Column(
                  children: [
                    buildCard(state.card),
                    buildBodyList(state.body)
                  ],
                );
              },
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              TextButton(onPressed: () {}, child: const Text('驳回')),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue, // 设置背景颜色
                  foregroundColor: Colors.white, // 设置文本颜色
                ),
                onPressed: () {
                  // ScaffoldMessenger.of(context).showSnackBar(httpToast);
                  templateBloc.fetch();
                },
                child: const Text('审批'),
              ),
            ],
          ),
        )
      ],
    );
  }
}
